﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Face8280WebSocketServer.Utility
{
    public class MD5Utility
    {
        public static string Encrypt(byte[] data)
        {
            if (data == null) return default;
            using MD5 md5 = MD5.Create();

            var hash = md5.ComputeHash(data);
            return Convert.ToHexString(hash);
        }

        public static string GetFileMD5(string sFile)
        {
            using MD5 md5 = MD5.Create();

            using FileStream sRead = new FileStream(sFile, FileMode.Open, FileAccess.Read);

            int iLen = (int)sRead.Length;
            byte[] md5Value = md5.ComputeHash(sRead);
            return Convert.ToHexString(md5Value);

        }
        public static string GetFileMD5(FileStream fileStream)
        {
            using MD5 md5 = MD5.Create();

            byte[] md5Value = md5.ComputeHash(fileStream);
            return Convert.ToHexString(md5Value);

        }

        public static string GetFileMD5(byte[] file)
        {
            using MD5 md5 = MD5.Create();

            byte[] md5Value = md5.ComputeHash(file);
            return Convert.ToHexString(md5Value);
        }
    }
}
